package org.dromara.quick.chat.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.IdGenerator;
import org.dromara.common.model.enums.channel.*;
import org.dromara.common.wukong.dto.ChannelBlacklistAddDTO;
import org.dromara.common.wukong.dto.ChannelBlacklistRemoveDTO;
import org.dromara.common.wukong.dto.ChannelSubscriberAddDTO;
import org.dromara.common.wukong.service.ChannelService;
import org.dromara.quick.chat.domain.ImChannelUser;
import org.dromara.quick.chat.mapper.ImChannelUserMapper;
import org.dromara.quick.chat.service.ImChannelUserService;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;

/**
* @author Stars
* @description 针对表【im_channel_user(频道用户表，记录频道的订阅者及其相关状态信息)】的数据库操作Service实现
* @createDate 2025-03-04 22:33:21
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class ImChannelUserServiceImpl extends ServiceImpl<ImChannelUserMapper, ImChannelUser>
    implements ImChannelUserService{

    private final ImChannelUserMapper imChannelUserMapper;

    private final ChannelService channelService;

    @Override
    public void addChannelUser(ImChannelUser imChannelUser) throws IOException {
        imChannelUserMapper.insert(imChannelUser);
        log.info("数据已经添加到频道用户表中，频道id为：{}，用户id为:{}",imChannelUser.getChannelId(),imChannelUser.getUserId());
        log.info("开始发送请求到悟空IM订阅群，频道Id也是频道id为：{}，用户id为：{}",imChannelUser.getChannelId(),imChannelUser.getUserId());
        ChannelSubscriberAddDTO channelSubscriberAdd = new ChannelSubscriberAddDTO();
        channelSubscriberAdd.setChannelId(imChannelUser.getChannelId());
        channelSubscriberAdd.setChannelType(imChannelUser.getType());
        channelSubscriberAdd.setReset(SubscriberResetStatusEnum.NOT_RESET.getCode());
        channelSubscriberAdd.setSubscribers(Collections.singletonList(imChannelUser.getUserId().toString()));
        channelSubscriberAdd.setTempSubscriber(ChannelTemporaryStatusEnum.NOT_TEMPORARY.getCode());
        channelService.addSubscribers(channelSubscriberAdd);
        log.info("结束发送请求到悟空IM订阅群，频道Id也是频道id为：{}，用户id为：{}",imChannelUser.getChannelId(),imChannelUser.getUserId());
    }

    @Override
    public void addChannelUser(String channelId, Long userId, ChannelTypeEnum channelType) throws IOException {
        ImChannelUser imChannelUser = new ImChannelUser();
        imChannelUser.setId(IdGenerator.getId());
        imChannelUser.setChannelId(channelId);
        imChannelUser.setUserId(userId);
        imChannelUser.setIsBlacklist(ChannelBlacklistStatusEnum.NOT_IN_BLACKLIST.getCode());
        imChannelUser.setIsWhitelist(ChannelWhitelistStatusEnum.NOT_IN_WHITELIST.getCode());
        imChannelUser.setIsBanned(ChannelIsBannedEnum.NOT_BANNED.getCode());
        imChannelUser.setType(channelType.getCode());
        imChannelUser.setCreateTime(new Date());
        imChannelUserMapper.insert(imChannelUser);
        log.info("数据已经添加到频道用户表中，频道id为：{}，用户id为:{}",imChannelUser.getChannelId(),imChannelUser.getUserId());
        log.info("开始发送请求到悟空IM订阅群，频道Id也是频道id为：{}，用户id为：{}",imChannelUser.getChannelId(),imChannelUser.getUserId());
        ChannelSubscriberAddDTO channelSubscriberAdd = new ChannelSubscriberAddDTO();
        channelSubscriberAdd.setChannelId(imChannelUser.getChannelId());
        channelSubscriberAdd.setChannelType(imChannelUser.getType());
        channelSubscriberAdd.setReset(SubscriberResetStatusEnum.NOT_RESET.getCode());
        channelSubscriberAdd.setSubscribers(Collections.singletonList(imChannelUser.getUserId().toString()));
        channelSubscriberAdd.setTempSubscriber(ChannelTemporaryStatusEnum.NOT_TEMPORARY.getCode());
        channelService.addSubscribers(channelSubscriberAdd);
        log.info("结束发送请求到悟空IM订阅群，频道Id也是频道id为：{}，用户id为：{}",imChannelUser.getChannelId(),imChannelUser.getUserId());
    }

    @Override
    public void addToBlacklist(String channelId, String userId, ChannelTypeEnum channelType) throws IOException {
        log.info("开始发送请求到悟空IM拉黑用户，频道Id也是频道id为：{}，用户id为：{}",channelId,userId);
        ChannelBlacklistAddDTO channelBlacklistAdd = new ChannelBlacklistAddDTO();
        channelBlacklistAdd.setChannelId(channelId);
        channelBlacklistAdd.setChannelType(channelType.getCode());
        channelBlacklistAdd.setUids(Collections.singletonList(userId));
        channelService.addToBlacklist(channelBlacklistAdd);
        log.info("结束发送请求到悟空IM拉黑用户，频道Id也是频道id为：{}，用户id为：{}",channelId,userId);
    }

    @Override
    public void removeFromBlacklist(String channelId, String userId, ChannelTypeEnum channelType) throws IOException {
        log.info("开始发送请求到悟空IM移除拉黑用户，频道Id也是频道id为：{}，用户id为：{}",channelId,userId);
        ChannelBlacklistRemoveDTO channelBlacklistRemove = new ChannelBlacklistRemoveDTO();
        channelBlacklistRemove.setChannelId(channelId);
        channelBlacklistRemove.setChannelType(channelType.getCode());
        channelBlacklistRemove.setUids(Collections.singletonList(userId));
        channelService.removeFromBlacklist(channelBlacklistRemove);
        log.info("结束发送请求到悟空IM移除拉黑用户，频道Id也是频道id为：{}，用户id为：{}",channelId,userId);
    }
}




